<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <!-- 引入最新版本的jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            background: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .welcome {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 5px;
        }
        .welcome p {
            font-size: 18px;
            margin: 10px 0;
        }
        .nav {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }
        .nav a {
            margin: 0 10px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 3px;
        }
        .nav a:hover {
            background-color: #45a049;
        }
        .loading {
            text-align: center;
            margin: 20px 0;
        }
        .message {
            margin: 15px 0;
            padding: 10px;
            border-radius: 3px;
            display: none;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>欢迎使用系统</h1>
        
        <div id="message-error" class="message error"></div>
        
        <div class="loading">
            加载中，请稍候...
        </div>
        
        <div class="welcome" style="display: none;">
            <p>欢迎回来，<span id="username">用户</span>！</p>
            <p>您已成功登录系统。</p>
        </div>
        
        <div class="nav">
            <a href="/view/user/index.html">用户中心</a>
            <a href="javascript:void(0);" id="logout-btn">退出登录</a>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            // 检查登录状态
            checkLoginStatus();
            
            // 退出登录按钮点击事件
            $('#logout-btn').on('click', function() {
                logout();
            });
            
            // 检查登录状态
            function checkLoginStatus() {
                $.ajax({
                    url: '/user/checkLogin',
                    type: 'GET',
                    dataType: 'json',
                    success: function(response) {
                        $('.loading').hide();
                        
                        if (response.code === 1) {
                            // 已登录，显示欢迎信息
                            $('#username').text(response.data.username);
                            $('.welcome').show();
                        } else {
                            // 未登录，跳转到登录页
                            window.location.href = '/view/user/login.html';
                        }
                    },
                    error: function() {
                        $('.loading').hide();
                        showError('网络错误，请稍后再试');
                        setTimeout(function() {
                            window.location.href = '/view/user/login.html';
                        }, 1500);
                    }
                });
            }
            
            // 退出登录
            function logout() {
                $.ajax({
                    url: '/user/logout',
                    type: 'POST',
                    dataType: 'json',
                    success: function(response) {
                        if (response.code === 1) {
                            // 退出成功，跳转到登录页
                            window.location.href = '/view/user/login.html';
                        } else {
                            showError(response.msg || '退出失败');
                        }
                    },
                    error: function() {
                        showError('网络错误，请稍后再试');
                    }
                });
            }
            
            // 显示错误消息
            function showError(message) {
                $('#message-error').text(message).show();
            }
        });
    </script>
</body>
</html>